package com.dataaccess.dataaccess.web.web;

import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseController;
import com.dataaccess.dataaccess.common.utils.appkit.FirefightingiotKit;
import com.dataaccess.dataaccess.config.rocketmq.UserProducer;
import com.dataaccess.dataaccess.support.annotaion.Action;
import com.dataaccess.dataaccess.support.annotaion.At;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.support.utils.ByteUtil;
import com.dataaccess.dataaccess.support.utils.HexUtils;
import com.dataaccess.dataaccess.support.utils.MyLogger;
import com.dataaccess.dataaccess.web.mapper.AlarmAllDeviceDataChangedMapper;
import com.dataaccess.dataaccess.web.service.AutoNetworkAlarmDeviceDataChangedService;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;

/**
 * 自动联网
 */
@Action
@RequestMapping("autoNet")
public class AutoNetController extends BaseController {

    @Resource
    public MyLogger logger;
    @Resource
    AutoNetworkAlarmDeviceDataChangedService autoNetworkAlarmDeviceDataChangedService;
    @Resource
    UserProducer userProducer;

    /**
     * 订阅--调用相关接口
     * @param
     * @return
     */
    @At(value = "device/getInfo", method = RequestMethod.POST)
    public RespEntity getInfo(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        return RespEntity.success();
    }

    /**
     * 回调地址--设备信息变化，订阅后推送设备信息变化通知
     * @param
     * @return
     */
    @At(value = "device/deviceInfoChanged", method = RequestMethod.POST)
    public RespEntity deviceInfoChanged(@RequestBody(required = false) JSONObject jsonParam) {
        return RespEntity.success();
    }

    /**
     * 回调地址：设备数据变化，订阅后推送设备数据变化通知
     * @param
     * @return
     */
    @At(value = "device/deviceDataChanged", method = RequestMethod.POST)
    public RespEntity deviceDataChanged(@RequestBody(required = false) JSONObject jsonParam) throws Exception  {
        logger.info("autoNetHX100"+"------"+jsonParam);
        jsonParam.put("deviceFlag","HX100");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            return autoNetworkAlarmDeviceDataChangedService.deviceDataChangedHX100(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }

    /**
     * 回调地址：设备数据变化，订阅后推送设备数据变化通知
     * @param
     * @return
     */
    @At(value = "device/deviceDataChangedSOS", method = RequestMethod.POST)
    public RespEntity deviceDataChangedSOS(@RequestBody(required = false) JSONObject jsonParam) throws Exception  {
        logger.info("deviceDataChangedSOS"+"------"+jsonParam);
        return autoNetworkAlarmDeviceDataChangedService.deviceDataChangedSOS(jsonParam);
    }


    /**
     * xulumin
     * 无线消防声光报警系统（派安PS-108 NB）
     * @param jsonParam
     * @return
     * @throws Exception
     */
    @At(value = "device/deviceDataHandSOS", method = RequestMethod.POST)
    public RespEntity deviceDataHandSOS(@RequestBody(required = false) JSONObject jsonParam) throws Exception  {
        logger.info("deviceDataChangedSOS"+"------"+jsonParam);
        return autoNetworkAlarmDeviceDataChangedService.deviceDataHandSOS(jsonParam);
    }

    /**
     * @Author YangYu
     * @Description sos一键报警（吉凯达打电话）
     * @Date Created in 13:52 2021/7/26
     * @Param [jsonParam]
     */
    @At(value = "device/deviceDataChangedJKDSOS", method = RequestMethod.POST)
    public RespEntity deviceDataChangedJKDSOS(@RequestBody(required = false) JSONObject jsonParam) throws Exception  {
        logger.info("deviceDataChangedJKDSOS"+"------"+jsonParam);
        return autoNetworkAlarmDeviceDataChangedService.deviceDataChangedJKDSOS(jsonParam);
    }

    /**
     * @Author YangYu
     * @Description sos一键报警（美阁移动式）
     * @Date Created in 13:52 2021/7/26
     * @Param [jsonParam]
     */
    @At(value = "device/deviceDataChangedMGSOS", method = RequestMethod.POST)
    public RespEntity deviceDataChangedMGSOS(@RequestParam MultiValueMap<String,Object> formData) throws Exception {
        logger.info("deviceDataChangedMGSOS"+"------"+formData.toString());
        return autoNetworkAlarmDeviceDataChangedService.deviceDataChangedMGSOS(formData);
    }

    /**
     * @Author YangYu
     * @Description 用户传输装置（大华）
     * @Date Created in 17:28 2023/9/25
     * @Param [jsonParam]
     */

    @At(value = "device/autoNetDahua", method = RequestMethod.POST)
    public RespEntity autoNetDahua(@RequestBody(required = false) JSONObject jsonParam) throws Exception  {
        logger.info("autoNetDahua"+"------"+jsonParam);
        String code = jsonParam.getStr("code");
        //返回指令
        String returnCode = "";
        returnCode = "03000102"+code.substring(12,24)+"640000000000"+code.substring(24,36)+"000003";
        String crc = HexUtils.checkSum(returnCode);
        returnCode ="4040"+returnCode+crc+"2323";
        jsonParam.put("deviceFlag","autoNetDahua");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            return autoNetworkAlarmDeviceDataChangedService.autoNetDahua(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success(returnCode);
    }

    public static void main(String[] args) {
        String crc = ByteUtil.getCRC(ByteUtil.hex2byte("93120410010D111A"));
        System.out.println(crc);
    }
    /**
     * 回调地址--设备信息变化，订阅后推送设备信息变化通知
     * @param
     * @return
     */
    @At(value = "device/JB_QB_F52", method = RequestMethod.POST)
    public RespEntity JB_QB_F52(@RequestBody(required = false) JSONObject jsonParam) {
        logger.info("JB_QB_F52"+"------"+jsonParam);
        jsonParam.put("deviceFlag","JB_QB_F52");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            return autoNetworkAlarmDeviceDataChangedService.JB_QB_F52(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }
}
